package com.yuanmei.views;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.yuanmei.common.Utils;
import com.yuanmei.model.StockItem;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class BlockListAdapter extends BaseAdapter {
    private final static String TAG = BlockListAdapter.class.getName();

    private Context mContext;
    private List<StockItem> mCurrentStockList;

    private final class ItemViewHolder {
        private TextView name;
        private TextView code;
        private TextView price;
        private TextView percent;
        private TextView vol_to_yesterday_percent;
        private TextView vol_to_ma5_percent;
        private TextView vol_to_ma20_percent;
        private TextView npr;
        private TextView per;
        private TextView industry;
    }

    BlockListAdapter(Context context, List<StockItem> stockList) {
        mContext = context;
        mCurrentStockList = stockList;
    }

    private boolean mIsStockCodeAscend = false;

    void sortCurrentListByStockCode() {
        mIsStockCodeAscend = !mIsStockCodeAscend;
        Collections.sort(mCurrentStockList, (o1, o2) -> {
            int res = o1.code().compareTo(o2.code());
            if (res > 0) {
                return mIsStockCodeAscend ? -1 : 1;
            } else if (res < 0) {
                return mIsStockCodeAscend ? 1 : -1;
            } else {
                return 0;
            }
        });
        notifyDataSetInvalidated();
    }

    private boolean mIsPriceSortAscend = false;
    void sortCurrentListByPricePercent() {
        mIsPriceSortAscend = !mIsPriceSortAscend;
        Collections.sort(mCurrentStockList, (o1, o2) -> {
            if (o1.getPercent() > o2.getPercent()) {
                return mIsPriceSortAscend ? -1 : 1;
            } else if (o1.getPercent() < o2.getPercent()) {
                return mIsPriceSortAscend ? 1 : -1;
            }
            return 0;
        });
        notifyDataSetInvalidated();
    }

    private boolean mIsVolumeSortAscend = false;

    void sortCurrentListByVolumePercent() {
        mIsVolumeSortAscend = !mIsVolumeSortAscend;
        Collections.sort(mCurrentStockList, (o1, o2) -> {
            if (o1.getTurnoverPercent() > o2.getTurnoverPercent()) {
                return mIsVolumeSortAscend ? -1 : 1;
            } else if (o1.getTurnoverPercent() < o2.getTurnoverPercent()) {
                return mIsVolumeSortAscend ? 1 : -1;
            }
            return 0;
        });
        notifyDataSetInvalidated();
    }

    private boolean mIsNetAssetOperateProfitRatioSortAscend = false;
    void sortCurrentListByNetAssetsOperateProfitRatio() {
        mIsNetAssetOperateProfitRatioSortAscend = !mIsNetAssetOperateProfitRatioSortAscend;
        Collections.sort(mCurrentStockList, (o1, o2) -> {
            if (o1.getNetAssetsOperatingProfitRatio() > o2.getNetAssetsOperatingProfitRatio()) {
                return mIsNetAssetOperateProfitRatioSortAscend ? -1 : 1;
            } else if (o1.getNetAssetsOperatingProfitRatio() < o2.getNetAssetsOperatingProfitRatio()) {
                return mIsNetAssetOperateProfitRatioSortAscend ? 1 : -1;
            }
            return 0;
        });
        notifyDataSetInvalidated();
    }

    private boolean mIsPriceEarningsRatioSortAscend = false;
    void sortCurrentListByPriceEarningsRatio() {
        mIsPriceEarningsRatioSortAscend = !mIsPriceEarningsRatioSortAscend;
        Collections.sort(mCurrentStockList, (o1, o2) -> {
            if (o1.getPriceEarningRatio() > o2.getPriceEarningRatio()) {
                return mIsPriceEarningsRatioSortAscend ? -1 : 1;
            } else if (o1.getPriceEarningRatio() < o2.getPriceEarningRatio()) {
                return mIsPriceEarningsRatioSortAscend ? 1 : -1;
            }
            return 0;
        });
        notifyDataSetInvalidated();
    }

    private boolean mIsIndustrySortAscend = false;
    void sortCurrentListByIndustry() {
        mIsIndustrySortAscend = !mIsIndustrySortAscend;
        Collections.sort(mCurrentStockList, (o1, o2) -> {
            int res = o1.getIndustryInfo().compareTo(o2.getIndustryInfo());
            if (res > 0) {
                return mIsIndustrySortAscend ? -1 : 1;
            } else if (res < 0) {
                return mIsIndustrySortAscend ? 1 : -1;
            } else {
                return 0;
            }
        });
        notifyDataSetInvalidated();
    }

    List<StockItem> getCurrentStockList() {
        return new ArrayList<>(mCurrentStockList);
    }
    void setCurrentStockList(List<StockItem> list) {
        mCurrentStockList = list;
    }

    @Override
    public int getCount() {
        return mCurrentStockList.size();
    }

    @Override
    public StockItem getItem(int index) {
        return mCurrentStockList.get(index);
    }

    @Override
    public long getItemId(int arg0) {
        return arg0;
    }

    private View createItemView() {
        ItemViewHolder holder = new ItemViewHolder();
        View itemView = LayoutInflater.from(mContext).inflate(
                R.layout.stock_list_item, null);
        holder.name = itemView.findViewById(R.id.name);
        holder.code = itemView.findViewById(R.id.code);
        holder.price = itemView.findViewById(R.id.price);
        holder.percent = itemView.findViewById(R.id.percent);
        holder.vol_to_yesterday_percent = itemView.findViewById(R.id.vol_to_yestoday);
        holder.vol_to_ma5_percent = itemView.findViewById(R.id.vol_to_ma5);
        holder.vol_to_ma20_percent = itemView.findViewById(R.id.vol_to_ma20);
        holder.npr = itemView.findViewById(R.id.npr);
        holder.per = itemView.findViewById(R.id.per);
        holder.industry = itemView.findViewById(R.id.industry);

        itemView.setTag(holder);
        return itemView;
    }

    @Override
    public View getView(int position, View itemView, ViewGroup parent) {
        StockItem item = getItem(position);
        if (item == null)
            return null;
        if (itemView == null) {
            itemView = createItemView();
        }
        // Update display
        ItemViewHolder holder = (ItemViewHolder) itemView.getTag();
        holder.name.setText(item.name());
        holder.code.setText(item.code());
        holder.price.setText(item.price());
        holder.percent.setText(item.pricePercent());
        holder.npr.setText(Utils.percentToDecimalString(item.getNetAssetsOperatingProfitRatio()));
        holder.per.setText(String.format("%.2f",item.getPriceEarningRatio()));
        holder.industry.setText(item.getIndustryInfo());

        float predictVolume = item.computePredictVolume();
        String vol2LastPct = item.getVolToLastVolumePercent(predictVolume);
        holder.vol_to_yesterday_percent.setText(vol2LastPct);
        String vol2Ma5pct = item.getVolToMa5Percent(predictVolume);
        holder.vol_to_ma5_percent.setText(vol2Ma5pct);
        String vol2Ma20pct = item.getVolToMa20Percent(predictVolume);
        holder.vol_to_ma20_percent.setText(vol2Ma20pct);

        return itemView;
    }

    int updateTodayQuotes(ArrayList<String> quotesList) {
        int count = 0;
        for (StockItem item : mCurrentStockList) {
            if (quotesList.contains(item.netEaseCode())) {
                item.updateTodayQuote();
                count++;
            }
        }
        return count;
    }

    void updateTodayMinuteTickQuote(String neCode) {
        for (StockItem item : mCurrentStockList) {
            if (neCode.contains(item.netEaseCode())) {
                item.rebuildTickQuotesFromRealm();
                break;
            }
        }
    }
}
